package com.xyh.transaction.controller;

import com.github.pagehelper.PageInfo;
import com.xyh.transaction.entity.Image;
import com.xyh.transaction.entity.dto.RotateDto;
import com.xyh.transaction.entity.dto.user.ArrDto;
import com.xyh.transaction.entity.vo.RotateVo;
import com.xyh.transaction.response.ResponseResult;
import com.xyh.transaction.service.ImageService;
import com.xyh.transaction.utils.ImageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "轮播图管理")
@RestController
@RequestMapping("/rotate")
public class RotateChartController {

    @Autowired
    private ImageService imageService;

    @ApiOperation(value = "轮播图", notes = "轮播图列表")
    @GetMapping("list")
    public ResponseResult<PageInfo<RotateVo>> list(RotateDto bannerDto) {
        PageInfo<RotateVo> bannerList = imageService.getRotateList(bannerDto);
        return ResponseResult.success("查询成功！", bannerList);
    }

    @ApiOperation(value = "添加轮播图", notes = "添加轮播图")
    @PostMapping("add")
    public ResponseResult<Void> add(@RequestBody RotateDto bannerDto) {
        imageService.addImage(bannerDto);
        return ResponseResult.success("操作成功！", null);
    }

    @ApiOperation(value = "编辑轮播图", notes = "编辑轮播图")
    @PostMapping("edit")
    public ResponseResult<Void> edit(@RequestBody RotateDto rotateDto) {
        Image image = new Image();
        image.setId(rotateDto.getId());
        image.setUrl(ImageUtils.getFileNameFromUrl(rotateDto.getImgUrl()));
        imageService.updateById(image);
        return ResponseResult.success("操作成功", null);
    }

    @ApiOperation(value = "删除轮播图", notes = "删除轮播图")
    @PostMapping("delete")
    public ResponseResult<Void> delete(@RequestBody ArrDto arrDto) {
        imageService.deleteById(arrDto);
        return ResponseResult.success("操作成功", null);
    }

}
